﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSE445_Assignment2_ChickenSales
{
     class cDecEnc
    {

             private String getString(cOrder newOrder)
             {
                 String output = "***" + newOrder.amount.ToString() + "***" + newOrder.cardNo.ToString() + "***" + newOrder.senderID.ToString() + "***" + newOrder.price + "***" + newOrder.date + "***"; 
                 return output;
             }

             private cOrder getOrderObject(String input)
             {
                 cOrder order = new cOrder();
                 string[] words1 = input.Split('*');
                 String[] words = new String[5];
                 int temp = 0;
                 for (int i = 0; i < words1.Length; i++)
                 {
                     if (words1[i] != "")
                     {
                         words[temp] = words1[i];
                         temp++;
                     }

                 }
                 order.amount = Convert.ToInt32(words[0]);
                 order.cardNo = Convert.ToInt32(words[1]);
                 order.senderID = Convert.ToInt16(words[2]);
                 order.price = Convert.ToInt32(words[3]);
                 order.date = Convert.ToDateTime(words[4]);
                 

                 return order;
             }
             private String Reverse(String input)
             {
                 String output = "";
                 char[] c = input.ToCharArray();
                 char[] invc = new char[c.Length];
                 for (int i = 0; i < c.Length; i++)
                 {
                     invc[c.Length - 1 - i] = c[i];
                 }
                 output = new String(invc);
                 return output;
             }

             public String Encode(cOrder newOrder)
             {
                 String output = getString(newOrder);

                 return Reverse(output);

             }

             public cOrder Decode(String input)
             {

                 cOrder order = new cOrder();
                 order = getOrderObject(Reverse(input));
                 return order;
             }

         }


    
}
